﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DesktopLauncher
{
    public partial class DesignerForm : Form
    {
        public DesignerForm()
        {
            InitializeComponent();
        }

        MenuItemExtended selectedItem = null;
        /// <summary>
        /// If right click - save selected item to selectedItem variable and show contextMenu
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void mainItem_MouseUp(object sender, MouseEventArgs e)
        {
            selectedItem = sender as MenuItemExtended;
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                if ((!selectedItem.HasDropDownItems) && (!String.IsNullOrEmpty(selectedItem.RunPath)) && (File.Exists(selectedItem.RunPath)))
                    System.Diagnostics.Process.Start(selectedItem.RunPath);
            }
            else if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                deleteMenuItem.Visible = selectedItem.Parrent != null;
                contextMenuManager.Show(Cursor.Position);
            }
        }

        private void renameMenuItem_Click(object sender, EventArgs e)
        {
            if (selectedItem == null) return;

            InputDialog id = new InputDialog();
            id.ShowDialog(ref selectedItem);
        }

        private void addMenuItem_Click(object sender, EventArgs e)
        {
            if (selectedItem == null) return;

            MenuItemExtended menuItem = new MenuItemExtended();

            InputDialog id = new InputDialog();
            if (id.ShowDialog(ref menuItem) == System.Windows.Forms.DialogResult.Abort) return;

            menuItem.MouseUp += mainItem_MouseUp;
            menuItem.Parrent = selectedItem;

            selectedItem.DropDownItems.Add(menuItem);
        }

        private void deleteMenuItem_Click(object sender, EventArgs e)
        {
            if (selectedItem == null) return;

            selectedItem.Delete();
        }

        private void generateMenuButton_Click(object sender, EventArgs e)
        {
            RegistryManager rm = new RegistryManager(desktopLauncherMenu.Items[0] as MenuItemExtended);
            rm.DeleteMenuFromRegistry();
            rm.RegisterDesktopLauncherItems();

            MessageBox.Show("Done!");
        }

        private void DesignerForm_Load(object sender, EventArgs e)
        {
            RegistryManager rm = new RegistryManager(this);
            desktopLauncherMenu.Items.Insert(0, rm.LoadMenuFromRegistry());
        }

        private void deleteMenuButon_Click(object sender, EventArgs e)
        {
            RegistryManager rm = new RegistryManager(this);
            rm.DeleteMenuFromRegistry();
            desktopLauncherMenu.Items.Clear();
            desktopLauncherMenu.Items.Insert(0, rm.LoadMenuFromRegistry());

            MessageBox.Show("Done!");
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Developed by: sczdavos [David Jančík], sczdavos@gmail.com");
        }
    }
}
